<?php

include "include/functions.inc.php";
/**
 * Constantes 
 */
define('IMPL', dirname(__FILE__) . "/modules/impl");
define('MODULES', dirname(__FILE__) . "/modules");
define('PORTLETS', dirname(__FILE__) . "/portlets");
define('PORTLETS_IMPL', dirname(__FILE__) . "/portlets/impl");
define('BO_LIBS', dirname(__FILE__) . "/bo");
define('PDF_FONTS', dirname(__FILE__) . "/fonts");
define('RELATIVE_PATH', "");
define('CORE_PATH', 'main.php');
define('AJAX_INDICATOR', 'ajax');
define('FORM_NAME', md5('easoftwareform'));
define('SITE_PATH', '/softcollegev3/');
define('IMAGES', dirname(__FILE__) . "/uploads/");
define('LOGO_PATH', "mhotivologo.png");
define('PATH_UPLOAD_STUDENTS', IMAGES);
define('MODULES_DB', '????mODULES????');
define('PORTLETS_DB', '????pORTLETS????');
define('APPWRAPPER', '????aPPwRAPPER????');
define('CFG_REPORTES_CTAS', "reportes/impl");
/**
 * Set librerias 
 */
// excel
include "include/excel.class.php";
include "include/mysql_excel.inc.php";
// pdf
include('include/pdf/config/lang/spa.php');
include "include/pdf/tcpdf.php";
//generals
include "include/widget.inc.php";
include "include/inputbuilder.inc.php";
include "include/easoftmodule.inc.php";
include "include/easoftportlet.inc.php";
include "include/easoftprinter.php";
include "include/EaExcelPrinter.php";
include "include/EaPdfPrinter.php";
include "include/easoftiframemodule.inc.php";
include "include/maintenance.inc.php";
include "include/fastmaintenance.inc.php";
include "include/complexmaintenance.inc.php";
include "include/jgridgenerator.class.php";
include "include/upload.class.php";
include "include/imagereader.php";
include "include/thumbnail_generator.php";
include "include/PortletFactory.php";
include "include/easoftwarebussinesobject.php";
include "include/eareportdoc.php";

includeModules(IMPL);
includeModules(PORTLETS_IMPL);
includeModules(MODULES . DS . CFG_REPORTES_CTAS);
includeModules(BO_LIBS);

$boSec = BoSecurity::getMe();

if (!isEmpty(getParam("action"))) {
    $boSec->registrarAccion("Salir del sistema!", "Ha solicitado salir del sistema");
    header('Location: logout.php');
}

$portlets = isset($_SESSION[PORTLETS_DB]) ? unserialize($_SESSION[PORTLETS_DB]) : array();
PortletFactory::$portletLog = $portlets;
$modName = getParam("module");

if (!isEmpty(getParam("portlet"))) {
    $porId = getParam("portlet");
    $portlet = PortletFactory::getPortlet(null, $porId);
    PortletFactory::savePortlets();
    $portlet->dispatchRequest();
} else {
    
    $module = null;
    $modules = array();

    if (isset($_SESSION[MODULES_DB])) {
        $modules = $_SESSION[MODULES_DB];
        $modules = unserialize($modules);
    }

    if (isset($modules[$modName])) {
        $module = $modules[$modName];
    } elseif (!isEmpty($modName)) {
        $module = new $modName();
        if ($module->includePath) {
            $module->includePath = MODULES . DS . $module->includePath;
        }
    }
    if (!defined("LINKS_PATH") && $module != null) {
        define("LINKS_PATH", CORE_PATH . "?" . AJAX_INDICATOR . "=true&module=" . $module->instanceName);
    }

    if (isEmpty(getParam(AJAX_INDICATOR))) {
        if (!isset($_SESSION["appwrap"])) {
            $appWrap = new AppWrapper();
        } else {
            $appWrap = unserialize($_SESSION["appwrap"]);
        }

        $appWrap->module = $module;
        $appWrap->db = DbManager::getMe();
        $appWrap->main();
        $_SESSION[APPWRAPPER] = serialize($appWrap);
    } else {
        $module->db = DbManager::getMe();
        $module->main();
    }
    if (!isEmpty(getParam("meac"))) {
        $boSec->registrarAccion("Acceso Opcion Menu", "Ha ingresado al modulo del sistema llamado: $modName");
    }
    $modules[$modName] = $module;
    $_SESSION[MODULES_DB] = serialize($modules);
}
?>